package com.lib.http.call_back;

import android.content.Context;

import com.google.gson.Gson;
import com.lib.bean.AnswerDetail;
import com.lib.fastkit.http.ok.EngineCallBack;
import com.lib.fastkit.http.ok.err.ResponseErrorListenerImpl;
import com.lib.fastkit.utils.log.LogUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class HttpTeacherAnswerDetailCallBack<T> implements EngineCallBack {

    private Context context;
    private ResponseErrorListenerImpl responseErrorListener;
    private static final int LOADING_TIME = 500;

    public HttpTeacherAnswerDetailCallBack() {

        responseErrorListener = new ResponseErrorListenerImpl();
    }

    @Override
    public void onPreExecute(final Context context, Map<String, Object> params) {
        // 大大方方的添加公用参数  与项目业务逻辑有关
        // 项目名称  context
//        params.put("app_name", "joke_essay");
//        params.put("version_name", "5.7.0");
//        params.put("ac", "wifi");
//        params.put("device_id", "30036118478");
//        params.put("device_brand", "Xiaomi");
//        params.put("update_version_code", "5701");
//        params.put("manifest_version_code", "570");
//        params.put("longitude", "113.000366");
//        params.put("latitude", "28.171377");
//        params.put("device_platform", "android");

        this.context = context;
        onPreExecute();
    }

    // 开始执行了
    public void onPreExecute() {


    }

    @Override
    public void onSuccess(String result) {

        AnswerDetail answerDetail = null;

        try {

            JSONObject jsonObject = new JSONObject(result);

            int code = jsonObject.getInt("code");

            if (200 == code) {

                answerDetail = new AnswerDetail();

                answerDetail.setCode(jsonObject.getInt("code"));

                JSONObject obj = jsonObject.getJSONObject("obj");


                AnswerDetail.ObjBean objBean = new AnswerDetail.ObjBean();


                objBean.setReply_type(obj.getInt("reply_type"));
                objBean.setReply_id(obj.getInt("reply_id"));
                objBean.setReply_total(obj.getInt("reply_total"));
                objBean.setUser_name(obj.getString("user_name"));
                objBean.setUser_icon(obj.getString("user_icon"));
                objBean.setIs_check(obj.getString("is_check"));
                objBean.setCorrect(obj.getString("correct"));
                objBean.setPraise(obj.getString("praise"));
                objBean.setIs_complaint(obj.getInt("is_complaint"));
                objBean.setIs_evaluate(obj.getInt("is_evaluate"));
                objBean.setTeacher_link(obj.getString("teacher_link"));

                JSONArray jsonArray1 = obj.getJSONArray("image");
                List<String> imgList1 = new ArrayList<>();
                for (int i = 0; i < jsonArray1.length(); i++) {
                    imgList1.add(jsonArray1.getString(i));

                }
                objBean.setImage(imgList1);


                JSONArray jsonArray2 = obj.getJSONArray("voice");
                List<String> imgList2 = new ArrayList<>();
                for (int i = 0; i < jsonArray2.length(); i++) {
                    imgList2.add(jsonArray2.getString(i));

                }
                objBean.setVoice(imgList2);

                JSONArray jsonArray3 = obj.getJSONArray("video");
                List<String> imgList3 = new ArrayList<>();
                for (int i = 0; i < jsonArray3.length(); i++) {
                    imgList3.add(jsonArray3.getString(i));

                }
                objBean.setVideo(imgList3);

                JSONArray jsonArray4 = obj.getJSONArray("video_screenshot");
                List<String> imgList4 = new ArrayList<>();
                for (int i = 0; i < jsonArray4.length(); i++) {
                    imgList4.add(jsonArray4.getString(i));

                }
                objBean.setVideo_screenshot(imgList4);
                answerDetail.setObj(objBean);
            }


            onSuccess((T) answerDetail);


        } catch (Exception e) {
            responseErrorListener.handleResponseError(context, e);

            LogUtil.e("HttpNormalCallBack-----------Json解析出错:" + e.getMessage());

        }

    }

    // 返回可以直接操作的对象
    public abstract void onSuccess(T result);

    @Override
    public void onError(Exception e) {

        try {
            //统一异常处理类
            responseErrorListener.handleResponseError(context, e);

            LogUtil.e(e.getMessage());

            onError(e.getMessage());

        } catch (Exception ex) {
            ex.printStackTrace();

        }


        //Toast.makeText(context, "请求超时", Toast.LENGTH_SHORT).show();


    }

    public abstract void onError(String e);

}
